package com.app.couponsharelife.ui.adapter

import androidx.databinding.DataBindingUtil
import android.view.LayoutInflater
import android.view.ViewGroup
import com.bumptech.glide.Glide
import com.app.couponsharelife.R
import com.app.couponsharelife.data.bean.SecondarySortBean
import com.app.couponsharelife.databinding.SortDetailListItemBinding
import com.app.couponsharelife.manage.Contacts
import com.app.couponsharelife.ui.listener.OnClickListener

/**
 * Created by guluwa on 2018/11/23.
 */
class SortDetailListAdapter(private var list: List<SecondarySortBean.ListBean>, private val listener: OnClickListener) :
        androidx.recyclerview.widget.RecyclerView.Adapter<androidx.recyclerview.widget.RecyclerView.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(DataBindingUtil.inflate(
                LayoutInflater.from(parent.context), R.layout.sort_detail_list_item, parent, false))
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onBindViewHolder(holder: androidx.recyclerview.widget.RecyclerView.ViewHolder, position: Int) {
        Glide.with(holder.itemView).asBitmap()
                .load(Contacts.BASEURL + list[position].advimg)
                .into((holder as ViewHolder).databinding.ivSortType)
        holder.databinding.tvSortType.text = list[position].name
    }

    inner class ViewHolder(val databinding: SortDetailListItemBinding) : androidx.recyclerview.widget.RecyclerView.ViewHolder(databinding.root) {
        init {
            databinding.root.setOnClickListener {
                if (adapterPosition != -1) {
                    listener.click(0, list[adapterPosition])
                }
            }
        }
    }
}